package org.akomantoso.core.data;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.akomantoso.core.io.Externalizable;
import org.akomantoso.core.io.Input;
import org.akomantoso.core.io.Output;

/**
 * <pre>
 * <xsd:element name="componentInfo">
 * 		<xsd:complexType>
 * 			<xsd:sequence minOccurs="0" maxOccurs="unbounded">
 * 				<xsd:element ref="componentData"/>
 *			</xsd:sequence>
 * 		</xsd:complexType>
 * </xsd:element>
 * </pre>
 * 
 * @author <a href="mailto:jacques.militello@gmail.com">Jacques Militello</a>
 * 
 */
public final class ComponentInfo implements Externalizable{

	public static final String ELEMENT = "componentInfo";
	
	private List<ComponentData> _componentData;

	/**
	 * @return the componentData
	 */
	public List<ComponentData> getComponentData() {
		return _componentData;
	}

	/**
	 * @param componentData the componentData to set
	 */
	public void setComponentData(List<ComponentData> componentData) {
		_componentData = componentData;
	}

	/**
	 * @see org.akomantoso.core.io.Externalizable#writeExternal(org.akomantoso.core.io.Output)
	 */
	@Override
	public <T extends Output> void writeExternal(T out) throws IOException {
		out.writeStart(ELEMENT);
		if (_componentData != null) {
			for (ComponentData data : _componentData) {
				data.writeExternal(out);
			}
		}
		out.writeEnd();
	}

	/**
	 * @see org.akomantoso.core.io.Externalizable#readExternal(org.akomantoso.core.io.Input)
	 */
	@Override
	public <T extends Input> void readExternal(T in) throws IOException {
		
		in.readNextStartElement();
		
		if (ComponentData.ELEMENT.equals(in.getLocalName())) {
			_componentData = new ArrayList<ComponentData>(4);
			ComponentData data;
			while (ComponentData.ELEMENT.equals(in.getLocalName())) {
				_componentData.add(data = new ComponentData());
				data.readExternal(in);
				in.readNextStartElement();
			}
		} 
		
	}

	
}